/*
get.cpp

Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2018 zjzengdongyang <http://zjzdy.cn>.

Permission is hereby  granted, free of charge, to any  person obtaining a copy
of this software and associated  documentation files (the "Software"), to deal
in the Software  without restriction, including without  limitation the rights
to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include "tmsg.hpp"

using namespace std;
using namespace std::chrono;

int main(int argc, char* argv[])
{
    string serverURI;
    if(argc > 1)
        serverURI = argv[1];
    if(serverURI.empty())
        serverURI = "tcp://localhost:1883";
    tmsg tm(serverURI);//初始化tmsg
    //连接服务器
    if(!tm.connect()) {
        cout << "Connect fail." << endl;
        return -1;
    }
    cout << "Topic:";
    string topic;
    cin >> topic;
    //订阅主题
    tm.subscribe(topic);
    //设置调试信息输出等级,默认不输出
    tm.setVerbose(4);
    //用来储存数据的变量
    string data;
    //消息指针
    std::shared_ptr<tmsg::msg> m;
    while(1) {
        //进程休眠,用来模拟其他动作,可将此值设大来测试消息到达时间
        this_thread::sleep_for(milliseconds(100));
        if(m != tm.getLatestMsg(topic)) {//获取该主题下最近的消息,忽略已获取过的消息
            m = tm.getLatestMsg(topic);
            if(m) {//判断消息是否为空
                auto time_diff = m->getTime() - steady_clock::now();//获得消息时间与现在时间之差,不受系统时间影响
                auto time = system_clock::to_time_t(system_clock::now() + duration_cast<system_clock::duration>(time_diff));//获得消息时的系统时间
                cout << "get msg time: " << std::put_time(std::localtime(&time), "%F %T")//打印获得消息时的系统时间
                     << " (" << std::chrono::duration_cast<std::chrono::milliseconds>(-time_diff).count() << "ms ago)"//打印时间差
                     << endl;
                //序号从0开始计算
                if(m->get(1, data))//获取该消息中序号为1的数据存放到data中, 并判断数据类型转换是否成功
                    cout << m->get<long long>("id") << ": " << data << endl;//获取该消息中标识为"id"的数据并转换成long long类型, 输出它与data
                if(m->dataCount() > 2)//判断该消息是否存在2个以上的数据
                    cout << "Ext data: " << m->getName(2) << ": " << m->get<long>(2) << endl;//获取该消息中序号为2的数据标识与数据并将数据转换成long long类型, 输出它们
            }
        }
    }
    return 0;
}
